package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbActiveRuleMapper;
import com.bwie.pojo.TbActive;
import com.bwie.pojo.TbActiveRule;
import com.bwie.service.TbActiveService;
import com.bwie.mapper.TbActiveMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_active(抽奖活动表)】的数据库操作Service实现
* @createDate 2025-07-29 09:28:19
*/
@Service
public class TbActiveServiceImpl extends ServiceImpl<TbActiveMapper, TbActive>
    implements TbActiveService{

    @Autowired
    TbActiveMapper tbActiveMapper;
    @Autowired
    TbActiveRuleMapper tbActiveRuleMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public R getActiveList(PageInfoVo pageInfoVo) {
        Page<TbActive> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        Page<TbActive> tbActivePage = tbActiveMapper.getActiveList(page, pageInfoVo);
        return R.OK(tbActivePage);
    }

    @Override
    public R addActive(TbActive tbActive) {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(404, "请先登录");
        }

        //处理开始时间和截止时间
        try {
            List<String> activeTime = tbActive.getActiveTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            if(activeTime!=null && activeTime.size()>0){
                tbActive.setBeginTime(format.parse(activeTime.get(0)));
                tbActive.setEndTime(format.parse(activeTime.get(1)));
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        //审计字段
        String userName = TokenUtil.userName(token);
        tbActive.setCreateBy(userName);
        tbActive.setCreateTime(new Date());

        tbActiveMapper.insert(tbActive);

        for (TbActiveRule tbActiveRule : tbActive.getRuleList()) {
            tbActiveRule.setActiveId(tbActive.getActiveId());

            //审计字段
            tbActiveRule.setCreateBy(userName);
            tbActiveRule.setCreateTime(new Date());

            tbActiveRuleMapper.insert(tbActiveRule);
        }
        return R.OK();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteActive(Integer activeId) {
        TbActive tbActive = tbActiveMapper.selectById(activeId);
        if(tbActive == null){
            return R.ERROR(404,"该活动不存在");
        }

        tbActiveRuleMapper.delete(
                new LambdaQueryWrapper<TbActiveRule>().eq(TbActiveRule::getActiveId,activeId)
        );
        tbActiveMapper.deleteById(activeId);
        return R.OK();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateActive(TbActive tbActive) {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(404, "请先登录");
        }
        String userName = TokenUtil.userName(token);
        TbActive active = tbActiveMapper.selectById(tbActive.getActiveId());
        if(active == null){
            return R.ERROR(404,"该活动不存在");
        }

        //处理开始时间和截止时间
        try {
            List<String> activeTime = tbActive.getActiveTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(activeTime != null && activeTime.size()>0){
                tbActive.setBeginTime(format.parse(activeTime.get(0)));
                tbActive.setEndTime(format.parse(activeTime.get(1)));
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        //审计字段
        tbActive.setUpdateBy(userName);
        tbActive.setUpdateTime(new Date());

        tbActiveMapper.updateById(tbActive);

        if(active.getRuleList()!=null && active.getRuleList().size()>0){
            for (TbActiveRule tbActiveRule : active.getRuleList()) {
                tbActiveRule.setActiveId(active.getActiveId());

                //审计字段
                tbActiveRule.setUpdateBy(userName);
                tbActiveRule.setUpdateTime(new Date());

                tbActiveRuleMapper.updateById(tbActiveRule);
            }

        }

        return R.OK();
    }

    @Override
    public R getActiveRuleList(Integer activeId) {
        TbActive tbActive = tbActiveMapper.selectById(activeId);
        if(tbActive == null){
            return R.ERROR(404,"该活动不存在");
        }

        List<TbActiveRule> tbActiveRules = tbActiveRuleMapper.selectList(
                new LambdaQueryWrapper<TbActiveRule>().eq(TbActiveRule::getActiveId, activeId)
        );
        return R.OK(tbActiveRules);
    }
}




